package me.frank.tman.ui.trade;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import me.frank.tman.R;
import me.frank.tman.model.StockModel;
import me.frank.tman.presenter.StockPresenter;

public class SetupFragment extends Fragment {


    protected final static String TAG = SetupFragment.class.getSimpleName();
    private AutoCompleteTextView mStockCode, mStockName;
    private EditText mStockPrice, mStockCost, mStockVolume;
    private TextView mHintText;
    private Button mSubmitBtn;

    private StockModel mStockModel;
    private StockPresenter mPresenter = StockPresenter.get();

    public void setStockModel(StockModel stockModel) {
        mStockModel = stockModel;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_setup, container, false);
        mStockCode = root.findViewById(R.id.et_stock_code);
        mStockName = root.findViewById(R.id.et_stock_name);
        mStockPrice = root.findViewById(R.id.et_stock_price);
        mStockCost = root.findViewById(R.id.et_stock_cost);
        mStockVolume = root.findViewById(R.id.et_stock_volume);
        mHintText = root.findViewById(R.id.tv_exchange_hint);
        mSubmitBtn = root.findViewById(R.id.btn_exchange_submit);

        ArrayAdapter<String> codeAdapter = new ArrayAdapter<String>(getContext(), R.layout.item_ac_text, R.id.tv_act_name, mPresenter.getStockCodes());
        mStockCode.setAdapter(codeAdapter);

        ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(getContext(), R.layout.item_ac_text, R.id.tv_act_name, mPresenter.getStockNames());
        mStockName.setAdapter(nameAdapter);

        mStockCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mStockCode.showDropDown();
                }
            }
        });
        mStockCode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = ((TextView) view).getText().toString();
                Log.d(TAG, "onItemClick:" + item);
                String[] parts = item.split("\t");

                mStockCode.setText(parts[0]);
                mStockName.setText(parts[1]);
            }
        });

        mStockName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mStockName.showDropDown();
                }
            }
        });

        mStockName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = ((TextView) view).getText().toString();
                Log.d(TAG, "onItemClick:" + item);
                String[] parts = item.split("\t");

                mStockName.setText(parts[0]);
                mStockCode.setText(parts[1]);
            }
        });


        if (mStockModel != null) {
            mStockCode.setText(mStockModel.code);
            mStockName.setText(mStockModel.code_name);
        }

        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String stockCode = mStockCode.getText().toString().trim();
                if (TextUtils.isEmpty(stockCode)) {
                    mHintText.setText("股票代码不能为空!");
                }

                String stockName = mStockName.getText().toString().trim();
                if (TextUtils.isEmpty(stockName)) {
                    mHintText.append(" 股票名称不能为空!");
                }

                float stockPrice = 0;
                try {
                    stockPrice = Float.parseFloat(mStockPrice.getText().toString().trim());
                } catch (NumberFormatException e) {
                    mHintText.append(" 股票价格异常!");
                }

                if (stockPrice <= 0) {
                    mHintText.append(" 股票价格必须大于0!");
                }

                float stockCost = 0;
                try {
                    stockCost = Float.parseFloat(mStockCost.getText().toString().trim());
                } catch (NumberFormatException e) {
                    mHintText.append(" 持仓成本异常!");
                }

                if (stockCost < 0) {
                    mHintText.append(" 股票价格必须大于等于0!");
                }

                int stockVolume = 0;
                try {
                    stockVolume = Integer.parseInt(mStockVolume.getText().toString().trim());
                } catch (NumberFormatException e) {
                    mHintText.append(" 交易数量异常!");
                }

                if (stockPrice / 100 <= 0) {
                    mHintText.append(" 交易数量必须为100的倍数!");
                }

                if (mHintText.getText().length() > 0) {
                    return;
                }

                if (submit(stockCode, stockName, stockPrice, stockCost, stockVolume)) {
                    mHintText.setText("");
                    mStockPrice.setText("");
                    mStockCost.setText("");
                    mStockVolume.setText("");
                }
            }
        });

        return root;
    }

    private boolean submit(String stockCode, String stockName, double stockPrice, double stockCost, int stockVolume) {

        return mPresenter.setup(stockCode, stockName, stockPrice, stockCost, stockVolume);
    }
}
